package com.HsqlDBTest;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewRecords extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Connection con = null;
	private DBConnection dbc = null;
	
	
    @Override
    public void init() throws ServletException {
    	dbc = new DBConnection();
    	con = dbc.getConnection();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            pst = con.prepareStatement("select * from contacts");
            pst.clearParameters();
            rs = pst.executeQuery();
            while(rs.next()){
                out.write("<br/>"+rs.getString(1));
                out.write(", "+rs.getString(2));
                out.write(", "+rs.getString(3));
            }
            out.write("<hr/><a href='index.html'>Home</a> ");
        } catch (SQLException e) {
            e.printStackTrace(System.out);
        }finally{
        	dbc.destroyConnection(rs, pst, con);
        }  
    }
}